package com.gemantic.glue.context;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * xxl-job context
 *
 * @author xuxueli 2020-05-21
 * [Dear hj]
 */
public class GlueExecContext {

    public static final int HANDLE_CODE_SUCCESS = 200;
    public static final int HANDLE_CODE_FAIL = 500;
    public static final int HANDLE_CODE_TIMEOUT = 502;

    // ---------------------- base info ----------------------

    /**
     * job id
     */
    private final long jobId;

    /**
     * job param
     */
    private final String jobParam;

    // ---------------------- for log ----------------------

    /**
     * job log filename
     */
    private final String jobLogFileName;

    // ---------------------- for shard ----------------------

    /**
     * shard index
     */
    private final int shardIndex;

    /**
     * shard total
     */
    private final int shardTotal;

    // ---------------------- for handle ----------------------

    /**
     * handleCode：The result status of job execution
     *
     *      200 : success
     *      500 : fail
     *      502 : timeout
     *
     */
    private int handleCode;

    /**
     * handleMsg：The simple log msg of job execution
     */
    private String handleMsg;

    /**
     * jobResult：exec result
     */
    private ConcurrentMap<String,Object> handlerResult = new ConcurrentHashMap<>();

    private ConcurrentMap<String,Object> pack = new ConcurrentHashMap<>();


    public GlueExecContext(long jobId, String jobParam, String jobLogFileName, ConcurrentMap<String,Object> pack) {
        this(jobId, jobParam, jobLogFileName, 0, 1, null, null, pack);
    }

    public GlueExecContext(long jobId, String jobParam, String jobLogFileName, int shardIndex, int shardTotal, String handleMsg, ConcurrentMap<String, Object> handlerResult, ConcurrentMap<String, Object> pack) {
        this.jobId = jobId;
        this.jobParam = jobParam;
        this.jobLogFileName = jobLogFileName;
        this.shardIndex = shardIndex;
        this.shardTotal = shardTotal;
        this.handleCode = HANDLE_CODE_SUCCESS;
        this.handleMsg = handleMsg;
        this.handlerResult = handlerResult;
        this.pack = pack;
    }

    public GlueExecContext(long jobId, String jobParam, String jobLogFileName, int shardIndex, int shardTotal, ConcurrentMap<String,Object> pack) {
        this(jobId, jobParam, jobLogFileName, shardIndex, shardTotal, null, null, pack);
    }

    public ConcurrentMap<String, Object> getHandlerResult() {
        return handlerResult;
    }

    public void setHandlerResult(ConcurrentMap<String, Object> handlerResult) {
        this.handlerResult = handlerResult;
    }

    public long getJobId() {
        return jobId;
    }

    public String getJobParam() {
        return jobParam;
    }

    public String getJobLogFileName() {
        return jobLogFileName;
    }

    public int getShardIndex() {
        return shardIndex;
    }

    public int getShardTotal() {
        return shardTotal;
    }

    public void setHandleCode(int handleCode) {
        this.handleCode = handleCode;
    }

    public int getHandleCode() {
        return handleCode;
    }

    public void setHandleMsg(String handleMsg) {
        this.handleMsg = handleMsg;
    }

    public String getHandleMsg() {
        return handleMsg;
    }

    public ConcurrentMap<String, Object> getPack() {
        return pack;
    }

    public void setPack(ConcurrentMap<String, Object> pack) {
        this.pack = pack;
    }

    // ---------------------- tool ----------------------

    private static InheritableThreadLocal<GlueExecContext> contextHolder = new InheritableThreadLocal<GlueExecContext>(); // support for child thread of job handler)

    public static void setXxlJobContext(GlueExecContext glueExecContext){
        contextHolder.set(glueExecContext);
    }

    public static GlueExecContext getXxlJobContext(){
        return contextHolder.get();
    }

}